/**
 * 动态添加事件
 */
export function addTinyPopupEvent(): void {
  // 查找全部.tinymce-popup
  const popups = document.querySelectorAll(".t-gen-form .tinymce-popup-btn");
  for (let i = 0; i < popups.length; i++) {
    // 先移除旧的事件处理程序
    popups[i].removeEventListener("click", popupClickHandler);

    // 添加点击事件
    popups[i].addEventListener("click", popupClickHandler);
  }
}

function popupClickHandler(e: any) {
  // 获取dom属性
  const phtml = e.target.getAttribute("phtml");
  // 创建
  const popup = document.createElement("div");
  popup.innerHTML = decodeURIComponent(phtml);
  popup.id = "tinymce-popup-show";
  popup.addEventListener("click", showPopupEleClick);
  // 添加到body
  document.body.appendChild(popup);
}

function showPopupEleClick() {
  const showPopupEle = document.getElementById("tinymce-popup-show");
  // 移除该dom
  if (showPopupEle) {
    showPopupEle.remove();
  }
}
